home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Containrs / sa / set_views < prev    next >
Text File  |  1996-04-09  |  3KB  |  85 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
  3. -- Copyright (C) 1995, International Computer Science Institute
  4. -- $Id: set_views.sa,v 1.5 1996/04/09 10:05:28 borisv Exp $
  5. --
  6. -- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY and is
  7. -- subject to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE
  8. -- contained in the file: Sather/Doc/License of the Sather
  9. -- distribution. The license is also available from ICSI, 1947 Center
  10. -- St., Suite 600, Berkeley CA 94704, USA.
  11. -- -------------------------------------------------------------------
  12. -- 
  13. --  Views of Sets
  14. --  -------------
  15. -- Several of the set operations return a "view" of the result
  16. -- instead of actually computing it.
  17. -- A view behaves just like any read-only set. However, it operates 
  18. -- by having pointers into the original sets, and hence will change 
  19. -- whenever the original sets change.  
  20. -- This creates a new set a_union_b, which has the actual elements of
  21. -- a and b.
  22. -- Views are defined by the classes BINOP_SET_VIEW and FILTER_SET_VIEW
  23. -- 
  24. -- The conventional set operations such as union and intersection
  25. -- are provided for convenience, but can be easily and more flexibly 
  26. -- achieved (with control over exactly what type of set is created) 
  27. -- using set views:
  28. --   a_union_b ::=  #SET{E}(a.union_view(b);
  29. --  
  30. -- Design notes on views:
  31. --       class MY_SET_VIEW{T} < $SET{T} is
  32. --            -- A particular implementation must define the following
  33. --            -- routines. See SET_INCL
  34. --            include SET_INCL{T};
  35. --            private create_from_internal(s: $ELT{T}): $SET{T} is 
  36. --                  ... ability to create a new set of the same type
  37. --            copy: SAME is ...
  38. --            has(e: E): BOOL is ...
  39. --            size: INT is ...
  40. --            elt!: E is ...
  41. --            insert(e: E);
  42. --            delete(e: E);
  43. --            clear;
  44. --       end;
  45. --  + Low space
  46. --  + Very fast to create - computations are performed at a query
  47. --  + Sometimes the operation need not be even computed
  48. --    (e.g. a.union_view(b).is_empty can return a result as long
  49. --          as there is a single element in either a or b without computing
  50. --          the union at all)
  51. --  + All the set operations may be performed on the result, including
  52. --   getting further views of the result.
  53. --  ? Different semantics
  54. --  - Slightly slower access (extra indirection that might be inlined
  55. --                            away once SAME works properly).
  56. --  - Much slower if set is going to be used repeatedly - instead,
  57. --    convert into a standard set by  #SET{E}(a.union_view(b))
  58.  
  59. class FILTER_SET_VIEW{ETP} < $RO_SET{ETP} is
  60.    include RO_SET_INCL{ETP};
  61.    
  62.    private attr pred: ROUT{ETP}:BOOL;
  63.    private attr source: $RO_SET{ETP};
  64.    
  65.    create(s: $RO_SET{ETP}, predicate: ROUT{ETP}:BOOL): SAME  pre ~void(s) is
  66.       res ::= new;
  67.       res.pred := predicate;
  68.       res.source := s;
  69.       return res;
  70.    end;
  71.    
  72.    copy: SAME is return create(source,pred) end;
  73.    -- Copy returns a copy of the same type of set
  74.    
  75.    has(e: ETP): BOOL is
  76.       if source.has(e) then return pred.call(e)  else return false; end;
  77.    end;
  78.    
  79.    elt!: ETP is
  80.       loop e ::= source.elt!; if pred.call(e) then yield e end; end;
  81.    end;
  82.     
  83. end;
  84. --=============================================================================
  85.